package org.astri.spitfire;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ListView;


import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;

import org.astri.spitfire.chart.MyColor;
import org.astri.spitfire.chart.listviewitems.ChartItem;
import org.astri.spitfire.chart.listviewitems.LineChartItem;
import org.astri.spitfire.entities.History;
import org.astri.spitfire.util.DataUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *     author : ghf
 *     e-mail : 869862783@qq.com
 *     time   : 2018/1/23
 *     desc   :
 *     modified by :
 *     e-mail : xx_xxx@xx
 *     time   :
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class HistoryActivity extends AppCompatActivity {

    public static float WIDTH = 3f;
    public static float CIRCLE_RADIUS = 9f;

    private static final String[] intervals = new String[]{"Daily", "Weekly", "Monthly"};

    private LineChart mChart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

//        LineChart chart = findViewById(R.id.chart);
//        chart.setData(genReadinessData());

        ListView lv = findViewById(R.id.listView1);

        ArrayList<ChartItem> list = new ArrayList<>();



        list.add(new LineChartItem(genReadinessData(), getApplicationContext()));
        list.add(new LineChartItem(genHR_HRV_SPO2Data(), getApplicationContext()));
        list.add(new LineChartItem(genGSRData(), getApplicationContext()));

        ChartDataAdapter cda = new ChartDataAdapter(getApplicationContext(), list);
        lv.setAdapter(cda);

    }

    /** adapter that supports 3 different item types */
    private class ChartDataAdapter extends ArrayAdapter<ChartItem> {

        public ChartDataAdapter(Context context, List<ChartItem> objects) {
            super(context, 0, objects);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return getItem(position).getView(position, convertView, getContext());
        }

        @Override
        public int getItemViewType(int position) {
            // return the views type
            return getItem(position).getItemType();
        }

        @Override
        public int getViewTypeCount() {
            return 3; // we have 3 different item-types
        }
    }

    /**
     * show chart with fake data
     * @param chart
     */
    private void setFakeData(LineChart chart){
        List<History> historyList = DataUtil.genFakeHistoryData();
        List<Entry> readiness_Val = new ArrayList<Entry>();
        List<Entry> HR_val = new ArrayList<Entry>();
        List<Entry> HRV_val = new ArrayList<Entry>();
        List<Entry> SPO2_val = new ArrayList<Entry>();
        List<Entry> GSR_val = new ArrayList<Entry>();
        final String[] xlabels = new String[10];

        for(int i=0; i<10; i++){
            History h = historyList.get(i);

            Entry readiness = new Entry(i, h.getReadiness());
            readiness_Val.add(readiness);

            Entry hr = new Entry(i, h.getHR());
            HR_val.add(hr);

            Entry hrv = new Entry(i, h.getHRV());
            HRV_val.add(hrv);

            Entry spo2 = new Entry(i, h.getSPO2());
            SPO2_val.add(spo2);

            xlabels[i] =  "" + i;
        }
        xlabels[0] = "01/11/2018";
        xlabels[9] = "01/20/2018";

        List<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
        LineDataSet readiness = new LineDataSet(readiness_Val, "Readiness");
        dataSets.add(readiness);

        LineDataSet hr = new LineDataSet(HR_val, "HR");
        dataSets.add(hr);

        LineDataSet hrv = new LineDataSet(HRV_val, "HRV");
        dataSets.add(hrv);

        LineDataSet spo2 = new LineDataSet(SPO2_val, "SPO2");
        dataSets.add(spo2);

        LineDataSet gsr = new LineDataSet(GSR_val, "GSR");
        dataSets.add(gsr);

        LineData data = new LineData(dataSets);
        chart.setData(data);
        chart.invalidate();

        IAxisValueFormatter formatter = new IAxisValueFormatter() {

            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return xlabels[(int) value];
            }

            // we don't draw numbers, so no decimal digits needed
//            @Override
//            public int getDecimalDigits() {  return 0; }
        };
        XAxis xAxis = chart.getXAxis();
        xAxis.setGranularity(1f); // minimum axis-step (interval) is 1
        xAxis.setValueFormatter(formatter);
    }


    private LineData genReadinessData(){
        List<History> historyList = DataUtil.genFakeHistoryData();
        List<Entry> readiness_Val = new ArrayList<Entry>();
        final String[] xlabels = new String[10];

        for(int i=0; i<10; i++){
            History h = historyList.get(i);

            Entry readiness = new Entry(i, h.getReadiness());
            readiness_Val.add(readiness);

            xlabels[i] =  "" + i;
        }
        xlabels[0] = "01/11/2018";
        xlabels[9] = "01/20/2018";

        List<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();

        LineDataSet readiness = new LineDataSet(readiness_Val, "Readiness");
        readiness.setLineWidth(WIDTH);
        readiness.setCircleRadius(CIRCLE_RADIUS);
        readiness.setHighLightColor(MyColor.SEA_GREEN);
        readiness.setColor(MyColor.SEA_GREEN);
        readiness.setCircleColor(MyColor.SEA_GREEN);
        readiness.setCircleColorHole(MyColor.SEA_GREEN);
        readiness.setDrawValues(false);

        dataSets.add(readiness);
        LineData data = new LineData(dataSets);


        return data;
    }

    private LineData genHR_HRV_SPO2Data(){

        List<History> historyList = DataUtil.genFakeHistoryData();
        List<Entry> HR_val = new ArrayList<Entry>();
        List<Entry> HRV_val = new ArrayList<Entry>();
        List<Entry> SPO2_val = new ArrayList<Entry>();
        final String[] xlabels = new String[10];

        for(int i=0; i<10; i++){
            History h = historyList.get(i);

            Entry hr = new Entry(i, h.getHR());
            HR_val.add(hr);

            Entry hrv = new Entry(i, h.getHRV());
            HRV_val.add(hrv);

            Entry spo2 = new Entry(i, h.getSPO2());
            SPO2_val.add(spo2);

            xlabels[i] =  "" + i;
        }
        xlabels[0] = "01/11/2018";
        xlabels[9] = "01/20/2018";

        List<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();

        LineDataSet hr = new LineDataSet(HR_val, "HR");
        hr.setLineWidth(WIDTH);
        hr.setDrawCircles(false);
        hr.setDrawValues(false);
        hr.setColor(MyColor.YELLOW);
        dataSets.add(hr);


        LineDataSet hrv = new LineDataSet(HRV_val, "HRV");
        hrv.setLineWidth(WIDTH);
        hrv.setDrawCircles(false);
        hrv.setDrawValues(false);
        hrv.setColor(MyColor.DARK_ORANGE);
        dataSets.add(hrv);

        LineDataSet spo2 = new LineDataSet(SPO2_val, "SPO2");
        spo2.setLineWidth(WIDTH);
        spo2.setDrawCircles(false);
        spo2.setDrawValues(false);
        spo2.setColor(MyColor.DEEP_SKY_BLUE2);
        dataSets.add(spo2);


        LineData data = new LineData(dataSets);


        IAxisValueFormatter formatter = new IAxisValueFormatter() {

            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return xlabels[(int) value];
            }

            // we don't draw numbers, so no decimal digits needed
//            @Override
//            public int getDecimalDigits() {  return 0; }
        };

        return data;
    }

    private LineData genGSRData(){

        List<History> historyList = DataUtil.genFakeHistoryData();
        List<Entry> GSR_val = new ArrayList<Entry>();
        final String[] xlabels = new String[10];

        for(int i=0; i<10; i++){
            History h = historyList.get(i);


            Entry gsr = new Entry(i, h.getGSR());
            GSR_val.add(gsr);

            xlabels[i] =  "" + i;
        }
        xlabels[0] = "01/11/2018";
        xlabels[9] = "01/20/2018";

        List<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();

        LineDataSet gsr = new LineDataSet(GSR_val, "GSR");
        gsr.setLineWidth(WIDTH);
        gsr.setDrawCircles(false);
        gsr.setDrawValues(false);
        gsr.setColor(MyColor.MAGENTA);

        dataSets.add(gsr);

        LineData data = new LineData(dataSets);


        IAxisValueFormatter formatter = new IAxisValueFormatter() {

            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return xlabels[(int) value];
            }

            // we don't draw numbers, so no decimal digits needed
//            @Override
//            public int getDecimalDigits() {  return 0; }
        };

        return data;
    }

}
